#include "../server.h"

int passwd_init()
{/*{{{*/
    return(0);
}/*}}}*/

SVC_FUNC(GETDBPASSWD)
{/*{{{*/
    char para_desc[50];
    PARA para;
    char sql[LONG_FILE_NM_SIZE+1];
    DBC_RSET rset;
    char dbname[NAME_SIZE+1];
    char dbuser[NAME_SIZE+1];
    char dbpasswd[NAME_SIZE+1];
    int res;

    sprintf(para_desc, "dbname(string:1-%d) dbuser(string:1-%d)",
            NAME_SIZE, NAME_SIZE);
    res=para_load(request, para_desc, &para);
    if(res)
    {
        if(res > 0)
            errlog("%s: request(%s) no.%d para invalid",
                    __func__, request, res);
        else
            errlog("%s: para_load request(%s) fail, res:%d",
                    __func__, request, res);
        para_put_item(respond, SOAR_ERR_PARA);
        return(atoi(SOAR_ERR_PARA));
    }
    para_copy_by_name(para, "dbname", dbname);
    para_copy_by_name(para, "dbuser", dbuser);
    para_free(para);

    sprintf(sql,
            "select passwd from db_passwd where "
            "name='%s' and user='%s'",  
            dbname, dbuser);
    res=sqlic_query(cnfg_info._db_.db, sql, 1, &rset);
    if(res != SQLI_OK)
    {
        errlog("%s: select db_passwd(%s,%s) fail, sqlcode:%d",
                __func__, dbname, dbuser, res);
        para_put_item(respond, SOAR_ERR_DB);
        return(atoi(SOAR_ERR_DB));
    }
    dbc_col_export_char(&rset, 0, 0, dbpasswd);
    dbc_query_free(&rset);

    para_put_item(respond, SOAR_ERR_OK);
    para_put_item(respond, dbpasswd);
    return(atoi(SOAR_ERR_OK));
}/*}}}*/

SVC_FUNC(GETPRIKEYPASSWD)
{/*{{{*/
    DBC_RSET rset;
    char passwd[NAME_SIZE+1];
    int res;

    res=sqlic_query(cnfg_info._db_.db,
            "select passwd from private_key_passwd", 1, &rset);
    if(res != SQLI_OK)
    {
        errlog("%s: select private_key_passwd fail, sqlcode:%d",
                __func__, res);
        para_put_item(respond, SOAR_ERR_DB);
        return(atoi(SOAR_ERR_DB));
    }
    dbc_col_export_char(&rset, 0, 0, passwd);
    dbc_query_free(&rset);

    para_put_item(respond, SOAR_ERR_OK);
    para_put_item(respond, passwd);
    return(atoi(SOAR_ERR_OK));
}/*}}}*/

SVC_FUNC(GETUSERPASSWDKEY)
{/*{{{*/
    DBC_RSET rset;
    char key[NAME_SIZE+1];
    int res;

    res=sqlic_query(cnfg_info._db_.db,
            "select key from user_passwd_key", 1, &rset);
    if(res != SQLI_OK)
    {
        errlog("%s: select user_passwd_key fail, sqlcode:%d",
                __func__, res);
        para_put_item(respond, SOAR_ERR_DB);
        return(atoi(SOAR_ERR_DB));
    }
    dbc_col_export_char(&rset, 0, 0, key);
    dbc_query_free(&rset);

    para_put_item(respond, SOAR_ERR_OK);
    para_put_item(respond, key);
    return(atoi(SOAR_ERR_OK));
}/*}}}*/
